package priv.pront.code.robot.roboCom_na_2022;

import java.io.*;
import java.util.*;

/**
 * @Description: 超时，只能用快读和快输
 * @Author: pront
 * @Time:2023-06-15 16:54
 */
public class RC_v1_智能管家 {

    static int n, m, t;
    static int[] a, q;
    static int cnt;
    static Map<Integer, Integer> map = new HashMap<>();
    static Map<Integer, Integer> map1 = new TreeMap<>();

    static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        st.nextToken();
        n = (int) st.nval;
        st.nextToken();
        m = (int) st.nval;

        a = new int[n];
        for (int i = 1; i <= n; i++) {
            st.nextToken();
            int target = (int) st.nval;
            map.put(i, target);
        }
        st.nextToken();
        t = (int) st.nval;
        while (t-- > 0) {
            map1 = new TreeMap<>();
            q = new int[100];
            cnt = 0;
            while(true){
                st.nextToken();
                int cur = (int) st.nval;
                if(cur == 0) break;
                q[cnt] = cur;
                cnt++;
            }
            for (int i = 0; i < cnt; i++) {
                int target = map.get(q[i]);
                if (!map1.containsKey(target)) {
                    map1.put(target, 1);
                } else {
                    map1.put(target, map1.get(target) + 1);
                }
            }
            String ss = "";
            for (Map.Entry<Integer, Integer> cur : map1.entrySet()) {
                int no = cur.getKey();
                int cnt = cur.getValue();
                ss += "B" + no + "-" + cnt + " ";
            }
            out.print(ss.substring(0, ss.length() - 1));
            if (t > 0) {
                out.println();
            }
            out.flush();
        }
    }
}
